package Servlet;

import Pojo.Page;
import Pojo.Student;
import Pojo.Page;
import Service.Service;
import Utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class StudentServlet extends BaseServlet {

    Service service=new Serviceimply();



    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.paraInt(req.getParameter("pageNo"), 0);
//        pageNo+=1;
//        String sno = req.getParameter("sno");
        String sname = req.getParameter("sname");
        String pwd = req.getParameter("pwd");
        String age = req.getParameter("age");
        String address = req.getParameter("address");
//        Student student = WebUtils.copyParamToBean(req.getParameterMap(), new Student());
//        service.login(student);
        service.add(new Student(null,sname,pwd,age,address,0));
        resp.sendRedirect(req.getContextPath()+"/studentServlet?action=pages&pageNo="+pageNo);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sno = req.getParameter("sno");
        service.delete(sno);
        resp.sendRedirect(req.getContextPath()+"/studentServlet?action=pages&pageNo="+req.getParameter("pageNo"));
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        String sno = req.getParameter("sno");
//        String sname = req.getParameter("sname");
//        String pwd = req.getParameter("pwd");
//        String age = req.getParameter("age");
//        String address = req.getParameter("address");

        Student student = WebUtils.copyParamToBean(req.getParameterMap(), new Student());
        service.update(student);
        resp.sendRedirect(req.getContextPath()+"/studentServlet?action=pages&pageNo="+req.getParameter("pageNo"));
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        List<Student> students = service.queryList();
        req.setAttribute("students",students);
        req.getRequestDispatcher("student_manager.jsp").forward(req,resp);
    }


    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sno = req.getParameter("sno");
        Student student = service.selectOne(Integer.valueOf(sno));
        req.setAttribute("student",student);
        req.getRequestDispatcher("student_Edit.jsp").forward(req,resp);
    }
    protected void pages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取请求参数 pageNo和pageSize对象
        int pageNo = WebUtils.paraInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.paraInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用service.page(pageNo,pageSize)
        Page<Student> page= service.page(pageNo,pageSize);
        req.setAttribute("page",page);
        req.getRequestDispatcher("student_manager.jsp").forward(req,resp);
    }
    protected void loginout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
            //销毁session
            req.getSession().invalidate();
            //重定向会登入页面
            req.getRequestDispatcher("loginoutSuccess.jsp").forward(req,resp);
    }


    protected void selectStudentInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Object id = session.getAttribute("id");
        int sno = WebUtils.paraInt(id.toString(), 0);
        Student studentInfo = service.selectOne(sno);
        req.setAttribute("studentInfo",studentInfo);
        req.getRequestDispatcher("studentAndteacher_manager.jsp").forward(req,resp);
    }

}
